package com.orangeforms.webadmin.app.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.orangeforms.common.core.annotation.MyDataSource;
import com.orangeforms.common.core.base.dao.BaseDaoMapper;
import com.orangeforms.common.core.base.service.BaseService;
import com.orangeforms.common.core.object.CallResult;
import com.orangeforms.common.core.object.MyRelationParam;
import com.orangeforms.common.core.util.MyModelUtil;
import com.orangeforms.common.sequence.wrapper.IdGeneratorWrapper;
import com.orangeforms.webadmin.app.dao.PaperExerciseTransMapper;
import com.orangeforms.webadmin.app.model.PaperExerciseTrans;
import com.orangeforms.webadmin.app.model.Student;
import com.orangeforms.webadmin.app.service.PaperExerciseTransService;
import com.orangeforms.webadmin.config.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * OrangeSingleDemo PaperExerciseTransServiceImpl class
 *
 * @author xisheng
 * @date 2022/06/02
 */
@Slf4j
@Service("paperExerciseTransService")
@MyDataSource(DataSourceType.MAIN)
public class PaperExerciseTransServiceImpl extends BaseService<PaperExerciseTrans,Long> implements PaperExerciseTransService {

    @Autowired
    private PaperExerciseTransMapper paperExerciseTransMapper;
    @Autowired
    private IdGeneratorWrapper idGenerator;

    /**
     * 返回当前Service的主表Mapper对象。
     *
     * @return
     */
    @Override
    protected BaseDaoMapper<PaperExerciseTrans> mapper() {
        return paperExerciseTransMapper;
    }

    @Override
    public boolean update(PaperExerciseTrans paperExerciseTrans, PaperExerciseTrans originPaperExerciseTrans) {
        MyModelUtil.fillCommonsForUpdate(paperExerciseTrans,originPaperExerciseTrans);
        UpdateWrapper<PaperExerciseTrans> uw = this.createUpdateQueryForNullValue(paperExerciseTrans,paperExerciseTrans.getTransId());
        return paperExerciseTransMapper.update(paperExerciseTrans,uw) == 1;
    }

    /**
     * 保存新增对象
     * @param trans
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PaperExerciseTrans saveNew(PaperExerciseTrans trans) {
        paperExerciseTransMapper.insert(this.buildDefaultValue(trans));
        return trans;
    }

    @Override
    public boolean remove(Long id) {
        return paperExerciseTransMapper.deleteById(id) == 1;
    }

    /**
     * 获取主表的查询结果，以及主表关联的字典数据和一对一从表数据，以及一对一从表的字典数据。
     * 该查询会涉及到一对一从表的关联过滤，或一对多从表的嵌套关联过滤，因此性能不如单表过滤。
     * 如果仅仅需要获取主表数据，请移步(getPaperExerciseTransList)，以便获取更好的查询性能。
     * @param filter 主表过滤对象。
     * @param orderBy 排序参数。
     * @return
     */
    @Override
    public List<PaperExerciseTrans> getPaperExerciseTransListWithRelation(PaperExerciseTrans filter, String orderBy) {
        List<PaperExerciseTrans> resultList = paperExerciseTransMapper.getPaperExerciseTransList(filter,orderBy);
        // 在缺省生成的代码中，如果查询结果resultList不是Page对象，说明没有分页，那么就很可能是数据导出接口调用了当前方法。
        // 为了避免一次性的大量数据关联，规避因此而造成的系统运行性能冲击，这里手动进行了分批次读取，开发者可按需修改该值。
        int batchSize = resultList instanceof Page ? 0 : 1000;
        this.buildRelationForDataList(resultList, MyRelationParam.normal(),batchSize);
        return resultList;
    }

    @Override
    public CallResult verifyRelatedData(PaperExerciseTrans trans, PaperExerciseTrans originalTrans){
        String errorMessageFormat = "数据验证失败，关联的%s并不存在，请刷新后重试！";
        return CallResult.ok();
    }


    private PaperExerciseTrans buildDefaultValue(PaperExerciseTrans trans){
        trans.setTransId(idGenerator.nextLongId());
        trans.setCommitTime(DateUtil.date());
        trans.setStudentId(idGenerator.nextLongId());
        trans.setCorrectFlag(0);
        trans.setDeviceType(0);
        trans.setPaperId(0L);
        trans.setExerciseId(0L);
        trans.setUsedSeconds(0);
        return trans;
    }

}
